<?php


/* SVN FILE: $Id: menu.php 105 2007-04-01 19:24:49Z Andy $ */

/**
 * Short description for file.php.
 *
 * Long description for file
 *
 * Copyright (c), Andy Dawson
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright    Copyright (c) 2007, Andy Dawson
 * @package      noswad
 * @subpackage   noswad.app.views.helpers
 * @since        Noswad site version 3
 * @todo         TODO Edit this block
 * @version      $Revision: 105 $
 * @created      26/01/2007
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2007-04-01 21:24:49 +0200 (dom, 01 abr 2007) $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Menu Helper class
 *
 *
 */
class MenuHelper extends Helper {
	/**
	 * Description for var
	 *
	 * @var array
	 * @access public
	 */
	var $helpers= array (
		'Html'
	);

	/**
	 * Switch to know if the generic processing has already been run
	 *
	 * @var boolean
	 * @access private
	 */
	var $_initialized= false;

	var $elementContents= '';
	var $menuElementCount= 0;
	var $narrowMenus= array ();
	var $wideMenus= array ();

	function generateTop($menuData) {
		return '<div id="menu">' .
		$this->generate('topMenu', $menuData[0], array (
			'class' => array (
				'menu',
				'tabbed'
			)
		)) .
		'</div>';
	}
	/**
	 * Short descrption
	 *
	 * @param string $id
	 * @param array $array
	 * @return void
	 */
	function generate($id, $menuData, $options= array ()) {
		$this->_setup();
		$return= '';
		$first= true;
		if (isset ($options['class'])) {
			$class= implode($options['class'], ' ');
		} else {
			$class= 'menu';
		}
		foreach ($menuData as $menuItem) {
			$Title= $menuItem['title'];
			$url= $menuItem['url'];
			if ($first) {
				$return .= '<ul class=\'' . $class . '\' id=\'' . $id . '\'>';
				$class= array (
					'first'
				);
				$first= false;
			} else {
				$class= array ();
			}
			$htmlOptions= array ();
			if (isset ($menuItem['active']) && $menuItem['active']) {
				$class[]= 'activeMenuAction';
				$return .= '<li class="' . implode(' ', $class) . '">';
				if ($this->_view->here == $url) {
					$return .= '<span>' . $Title . '</span>';
				} else {
					$return .= $this->Html->link($Title, $url, $htmlOptions, NULL, false);
				}
			} else {
				if ($class) {
					$return .= '<li class="' . implode(' ', $class) . '">';
				} else {
					$return .= '<li>';
				}
				$return .= $this->Html->link($Title, $url, $htmlOptions, NULL, FALSE);
			}
			$return .= '</li>';
		}
		if ($return) {
			$return .= '</ul>';
		}
		elseif (isset ($options['displayEmpty'])) {
			$return= '<ul class=\'menu\' id=\'' . $id . '\'><li></li></ul>';
		}
		return $return;
	}

	function processElements($menuContext, $data) {
		if (isset ($this->_view->plugin)) {
			$pluginPathPrefix= APP . DS . 'plugins' . DS . $this->_view->plugin . DS . 'views' . DS . 'elements' . DS;
		}
		$appPathPrefix= APP . DS . 'views' . DS . 'elements' . DS;
		foreach ($menuContext as $Title => $elementData) {
			$elementFile= Inflector :: underscore($Title);
			// allow overriding of where the element is placed
			// E.g. to set the element to be rendered after the thin menu, use a value of 999
			if (isset ($elementData['sequence'])) {
				$sequence= $elementData['sequence'];
			} else {
				$sequence= $this->menuElementCount;
				$this->menuElementCount++;
			}

			if (1 == 2) { // Check if already cached

			} else {
				//$this->startCaching($elementFile, $elementData);
				// Plugin wide element
				if ((isset ($this->_view->plugin)) && (file_exists($pluginPathPrefix . $elementFile . '_wide.ctp'))) {
					$file= $pluginPathPrefix . $elementFile . '_wide.ctp';
					$wide= true;
					// Wide element
				}
				elseif (file_exists($appPathPrefix . $elementFile . '_wide.ctp')) {
					$file= $appPathPrefix . $elementFile . '_wide.ctp';
					$wide= true;
					// Plugin element
				}
				elseif ((isset ($this->_view->plugin)) && (file_exists($pluginPathPrefix . $elementFile . '.ctp'))) {
					$file= $pluginPathPrefix . $elementFile . '.ctp';
					$wide= false;
					// Element
				}
				elseif (file_exists($appPathPrefix . $elementFile . '.ctp')) {
					$file= $appPathPrefix . $elementFile . '.ctp';
					$wide= false;
					// No Element, just data
				} else {
					$file= false;
				}
				if ($file) {
					if ($wide) {
						$this->wideMenus[$sequence]= $this->_view->renderElement($elementFile . '_wide', array (
							'data' => $elementData,
							'contentData' => $data
						));
						$this->menuElementCount++;
					} else {
						$this->narrowMenus[$this->menuElementCount]= $this->_view->renderElement($elementFile, array (
							'data' => $elementData
						));
						$this->menuElementCount++;
					}
				}
				elseif ($elementData) {
					$this->processGenericElement($Title, $elementData);
				}
				$this->stopCaching($elementFile, $elementData);
			}
		}
		foreach ($this->wideMenus as $id => $wideMenu) {
			$menus[$id]= '<div class =\'side_container_wide\'><div class=\'menuSectionWide\'>' . $wideMenu . '</div></div>';
		}

		$menus= array ();
		$MCounter= 0;
		$NCount= count($this->narrowMenus);
		foreach ($this->narrowMenus as $id => $Menu) {
			$MCounter++;
			$this->menuElementCount++;
			if (($MCounter % 2) != 0) {
				$menus[$this->menuElementCount]= '<div class =\'side_container\'><div class=\'menuSection menuSectionLeft\'>' . $Menu . '</div>';
				if ($MCounter == $NCount) { // There is no sub menu to follow, close up the div
					$menus[$this->menuElementCount] .= '</div>';
				}

			} else {
				$menus[$this->menuElementCount]= '<div class=\'menuSection menuSectionRight\'>' . $Menu . '</div></div>';
			}
		}
		ksort($menus);
		return implode('', $menus);
	}

	function processGenericElement($Title, $elementData) {
		$contents= "<h3>$Title</h3><div class='menuSectionContents'><ul>\r\n";
		foreach ($elementData as $index => $menuItem) {
			if (isset ($menuItem['url'])) {
				$title= isset ($menuItem['title']) ? $menuItem['title'] : $index;
				$contents .= '<li>';
				if (isset ($menuItem['active'])) {
					if ($this->_view->here == $menuItem['url']) {
						$contents .= '<strong><span class="activeMenuAction">' . $title . '</span></strong>';
					} else {
						$contents .= $this->Html->link('<span class="activeMenuAction">' . $title . '</span>', $menuItem['url'], null, null, FALSE);
					}
				} else {
					$contents .= $this->Html->link($title, $menuItem['url'], null, null, FALSE);
				}
				$contents .= "</li>\r\n";
			}
		}
		$contents .= '</ul></div>';
		$this->menuElementCount++;
		return $contents;
	}

	function startCaching() {
		ob_start();
	}

	function stopCaching() {
		$output= ob_get_clean();
		$view= & $this->_view;
	}

	function _setup() {
		if (!$this->_initialized) {
			$this->_view= & ClassRegistry :: getObject('view');
			$this->_initialized= true;
		}
	}
}
?>